package com.goodving.guli.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.DimPanelFragment;
import com.goodving.guli.bt.eventbus.EventPersonInfoUpdate;
import com.goodving.guli.bt.eventbus.EventUserInfoUpdate;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.ui.PickAdapter;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.C4220a;

import java.util.Calendar;

import de.greenrobot.event.EventBus;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.WheelViewAdapter;

/**
 * Created by john on 2015/12/24.
 */
public class AgePanelFragment extends DimPanelFragment { //db
    private static final String f9916h;
    private static int f9917j;
    private PersonInfo f9918a;
    private UserInfo f9919b;
    private WheelView f9920c;
    private WheelView f9921d;
    private Birthday f9922e;
    private int f9923f;
    private int f9924g;
    private PickAdapter f9925i;

    static {
        f9916h = AgePanelFragment.class.getSimpleName();
    }

    public static Fragment m14642a(int i) {
        GuliLog.tracePrintInfo(f9916h, "get the uid " + i);
        f9917j = i;
        return new AgePanelFragment();
    }

    protected int inflateLayout() {
        return R.layout.fragment_person_info_setting_birthday;
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (f9917j == -1) {
            this.f9918a = Keeper.readPersonInfo();
            this.f9922e = Birthday.fromStr(this.f9918a.getBirthday());
            return;
        }
        this.f9919b = UserInfoManager.getInstance().getInfoByUid(f9917j);
        this.f9922e = Birthday.fromStr(this.f9919b.birthday);
    }

    class dc implements OnWheelScrollListener {
        final /* synthetic */ AgePanelFragment f9926a;

        dc(AgePanelFragment dbVar) {
            this.f9926a = dbVar;
        }

        public void onScrollingStarted(WheelView wheelView) {
            GuliLog.tracePrintInfo(AgePanelFragment.f9916h, "onScrollingStarted " + wheelView.getRealCurrentItem());
        }

        public void onScrollingFinished(WheelView wheelView) {
            int i = 0;
            GuliLog.tracePrintInfo(AgePanelFragment.f9916h, "onScrollingFinished " + wheelView.getRealCurrentItem());
            if (100 == wheelView.getRealCurrentItem()) {
                GuliLog.tracePrintInfo(AgePanelFragment.f9916h, " should refresh month");
                if (this.f9926a.isAdded()) {
                    WheelViewAdapter dmVar = new PickAdapter(this.f9926a.getActivity(), 1, this.f9926a.f9924g + 1, this.f9926a.f9921d, this.f9926a.getResources().getColor(R.color.highlight), this.f9926a.getResources().getColor(R.color.main_ui_content_color), C4220a.bm, false, 46, 24, 21, 21, 1);
                    WheelView b = this.f9926a.f9921d;
                    if (this.f9926a.f9924g + 1 < 0) {
                        i = this.f9926a.f9924g + 1;
                    }
                    b.setCurrentItem(i);
                    this.f9926a.f9921d.setViewAdapter(dmVar);
                    return;
                }
                return;
            }
            this.f9926a.f9921d.setViewAdapter(this.f9926a.f9925i);
        }
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View onCreateView = super.onCreateView(layoutInflater, viewGroup, bundle);
        this.f9920c = (WheelView) onCreateView.findViewById(R.id.setting_birth_wheel_year);
        this.f9921d = (WheelView) onCreateView.findViewById(R.id.setting_birth_wheel_month);
        int i = Calendar.getInstance().get(Calendar.YEAR);
        this.f9924g = Calendar.getInstance().get(Calendar.MONTH);
        GuliLog.tracePrintInfo(f9916h, "maxMonth is " + this.f9924g);
        this.f9923f = i - 100;
        int i2 = i + 0;
        WheelView a = this.f9920c.setVisibleItems(5).setCenterDrawable(R.drawable.wheel_custom_val_white_1).setCenterStyle(getString(R.string.year), 40.0f);
        a.setViewAdapter(new PickAdapter(getActivity(), this.f9923f, i2, this.f9920c, getResources().getColor(R.color.highlight), getResources().getColor(R.color.main_ui_content_color), C4220a.bm, false, 46, 24, 21, 21, 1));
        this.f9925i = new PickAdapter(getActivity(), 1, 12, this.f9921d, getResources().getColor(R.color.highlight), getResources().getColor(R.color.main_ui_content_color), C4220a.bm, true, 46, 24, 21, 21, 1);
        if (!this.f9922e.isValid() || this.f9922e.getYear() - this.f9923f < 100) {
            this.f9921d.setVisibleItems(5).setCenterDrawable(R.drawable.wheel_custom_val_white_1).setCenterStyle(getString(R.string.month), (float) WeightUtil.f11306h).setViewAdapter(this.f9925i);
        } else {
            this.f9921d.setVisibleItems(5).setCenterDrawable(R.drawable.wheel_custom_val_white_1).setCenterStyle(getString(R.string.month), (float) WeightUtil.f11306h).setViewAdapter(new PickAdapter(getActivity(), 1, this.f9924g + 1, this.f9921d, getResources().getColor(R.color.highlight), getResources().getColor(R.color.main_ui_content_color), C4220a.bm, false, 46, 24, 21, 21, 1));
        }
        if (this.f9922e.isValid()) {
            this.f9920c.setCurrentItem(this.f9922e.getYear() - this.f9923f);
            this.f9921d.setCurrentItemLooped(this.f9922e.getMonth() - 1);
        } else {
            this.f9920c.setCurrentItem(1990 - this.f9923f);
            this.f9921d.setCurrentItemLooped(0);
        }
        this.f9920c.addScrollingListener(new dc(this));
        return onCreateView;
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.f3566i);
    }

    public void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.f3566i);
    }

    protected void onRightButtonClicked() {
        String birthday;
        if (this.f9918a != null) {
            birthday = this.f9918a.getBirthday();
            this.f9922e.setYear(this.f9920c.getRealCurrentItem() + this.f9923f);
            this.f9922e.setMonth(this.f9921d.getRealCurrentItem() + 1);
            if (this.f9922e.toStringData().equals(birthday)) {
                dismiss();
                return;
            }
            this.f9918a.setBirthday(this.f9922e.toStringData());
            this.f9918a.setAge(this.f9922e.getAge());
            this.f9918a.setNeedSyncServer(2);
            Keeper.keepPersonInfo(this.f9918a);
            EventBus.getDefault().post(new EventPersonInfoUpdate(birthday));
        } else {
            birthday = this.f9919b.birthday;
            this.f9922e.setYear(this.f9920c.getRealCurrentItem() + this.f9923f);
            this.f9922e.setMonth(this.f9921d.getRealCurrentItem() + 1);
            this.f9919b.birthday = this.f9922e.toStringData();
            if (this.f9922e.toStringData().equals(birthday)) {
                dismiss();
                return;
            }
            this.f9919b.birthday = this.f9922e.toStringData();
            EventBus.getDefault().post(new EventUserInfoUpdate(this.f9919b));
        }
        dismiss();
    }
}
